﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 4/8/2009
 * Time: 9:48 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using popLib;

namespace CloseTimer
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		int total = 0;
		int org = 0;
		string startApp = "";
		string argument = "";
		public MainForm(string[] args)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			if(args.Length > 0 && StringLib.isValid(args[0])){
				try{
					org = total = NumberLib.getInt(args[0]);
					if( args.Length >=2 && StringLib.isValid(args[1]) )
						org = total = NumberLib.getInt(args[0]) + ( NumberLib.getInt(args[1]) * 60 );
					if( args.Length >= 3 )
						startApp = args[2];
					if( args.Length >= 4 )
						argument = args[3].Replace("'","\"");
				}catch(Exception ex){ex.ToString();}
			}						
			timer1.Start();			
		}
		
		void Timer1Tick(object sender, EventArgs e)
		{
			if(timer1.Interval < 1000){
				timer1.Interval = 1000;
				this.Visible = false;
				notifyIcon1.Visible = true;
			}else{
				
				total--;
				
				if( total <= 30 && total > -1 && total % 5 == 0 )
					notifyIcon1.ShowBalloonTip(15, "CloseTimer", "Seconds left to close: "+total, ToolTipIcon.Info );
				
				notifyIcon1.Text = "CloseTimer - "+total+" seconds left";
				
				if(total <= 0){
					if( StringLib.isValid(startApp) ){
						try{
							if( StringLib.isValid(argument) )
								System.Diagnostics.Process.Start( startApp, argument );
							else System.Diagnostics.Process.Start( startApp );
						}catch{}
					}
					this.Close();
				}
			}
		}
		
		void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			notifyIcon1.Visible = false;
		}
		
		void ResetToolStripMenuItemClick(object sender, EventArgs e)
		{
			total = org;
		}
		
		void CloseToolStripMenuItemClick(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
